package com.turing.data.scripts.zjtq.filter

import com.turing.common.util.FileUtils
import com.turing.common.util.Log
import com.turing.common.util.StringUtils
import com.turing.data.engine.TextUtils
import com.turing.data.engine.utils.ScriptsUtils
import com.turing.data.vo.out.review.DependParamsVO
import com.turing.data.vo.out.review.TaskElementVO
import com.turing.data.engine.utils.RegexExtract;

import java.text.DecimalFormat

/**
 * 场景名称：追加提取单
 * 要素名称：金额小写
 **/

class AmountInFigures_30230_34_filter {

    static void main(String[] args) {
        TaskElementVO.DrawResultVO resultVO = new TaskElementVO.DrawResultVO()
        resultVO.text = "1100.56"
        replacePoint(resultVO)
    }

    static void replacePoint(TaskElementVO.DrawResultVO resultVO){
        try {
            String resultValue = resultVO.text
            char[] resultCharArray = resultValue.toCharArray()
            int arrayLength = resultCharArray.length
            StringBuilder sb = new StringBuilder()
            boolean hasPointFlag = true

            if(arrayLength > 3){
                hasPointFlag = resultCharArray[arrayLength - 3].equals('.')
            }
            println("AmountInFigures_hasPointFlag="+hasPointFlag)

            if(resultValue =~ /\d{1,8}.\d{2}/){
                return
            }

            println("AmountInFigures_resultValue1="+resultValue)
            resultValue = resultValue.replace(".","").replace(",","")
            println("AmountInFigures_resultValue2="+resultValue)
            if (!StringUtils.isEmpty(resultValue)) {
                String value = "0.0"
                if(hasPointFlag){
                    int len = resultValue.length()
                    value = resultValue.substring(0,len-2) + ".00"
                }else{
                    value = resultValue
                }

                double number = Double.parseDouble(value)
                DecimalFormat df = new DecimalFormat("#,###")
                String str = df.format(number)

                str = str + ".00"

                println("AmountInFigures_str="+str)
                resultVO.text = str
                resultVO.businessValue = str
                resultVO.type = 1
            }

        } catch (Exception e) {
            e.printStackTrace()
        }
    }

    static List<TaskElementVO.DrawResultVO> doConvert(DependParamsVO dependParams) {
        List<TaskElementVO.DrawResultVO> trueValueSet = new ArrayList<>()
        def REGEX_BACKUP = /提取/
        List<TaskElementVO.DrawResultVO > resultVOs = ScriptsUtils.tryInitResult(dependParams)
        String businessValue = resultVOs.get(0).getBusinessValue()
        if(!StringUtils.isEmpty(businessValue) ){
            if (resultVOs!=null && resultVOs.size()>0) {
                for (int i = 0; i < resultVOs.size(); i++) {
                    def drawResultVO = resultVOs.get(i)
                    def childTaskSn = drawResultVO.childTaskSn
                    if (null == childTaskSn || childTaskSn == "") {
                        continue
                    }
                    def start = drawResultVO.start
//                    def end = drawResultVO.end
//                    def text = drawResultVO.text
                    def textPathMap = dependParams.textPathMap
                    def rawPath = textPathMap.get(drawResultVO.childTaskSn)
                    def rawText = FileUtils.loadJson(rawPath)
                    def expandOffset = 0
                    while (expandOffset < 20 && start > expandOffset) {
                        def expandString = rawText.substring(start - expandOffset, start)
                        if (expandString =~ REGEX_BACKUP) {
                            trueValueSet.clear()
                            trueValueSet.add(drawResultVO)
                            return trueValueSet
                        }
                        expandOffset++
                    }
                }
            }
        }


        String REGEX = /(?<=人民币.{1,10})\d+.{0,10}(?=】元)/
//            Log.info("AmountInFigures_11438_34_filter_JSONTextMap="+dependParams.JSONTextMap)
        dependParams.JSONTextMap.forEach(
                { childTaskSn, jsonText ->
                    RegexExtract.doRuleExtract(REGEX,childTaskSn, dependParams, jsonText, trueValueSet)
                }
        )
//            Log.info("AmountInFigures_11438_34_filter_trueValueSet="+trueValueSet)

        if(trueValueSet.size() == 0){
            REGEX = /(?<=人民币.{0,10})\d+(?=元)/
            dependParams.JSONTextMap.forEach(
                { childTaskSn, jsonText ->
                    RegexExtract.doRuleExtract(REGEX,childTaskSn, dependParams, jsonText, trueValueSet)
                }
            )
        }

        if(trueValueSet.size() == 0){
            TaskElementVO.DrawResultVO drawResult = new TaskElementVO.DrawResultVO()
            drawResult.text = ""
            trueValueSet.clear()
            trueValueSet.add(drawResult)
        }

        return trueValueSet
//        }
//        else{
//            return drawResultList
//        }
    }

    static String exchangeStr( String amount ){
        def s = amount.replaceAll(",", "").replaceAll("，","").replaceAll("\\.", "").replaceAll("。","")
        return s
    }
}
